/*
 * @(#)WindowTableModel.java
 */
package com.pluginsacceptor.plugin.spreadsheet.table;

import org.apache.log4j.Logger;

import com.pluginsacceptor.plugin.spreadsheet.model.Cell;


/**
 * Custom table model
 */
public class SSTableModel extends javax.swing.table.AbstractTableModel {

    /** Logger */
    private static final Logger LOG = Logger.getLogger(SSTableModel.class);


    /** */
    private final int numRow;

    /** */
    private final int numCol;

    /** */
    private final Cell[][] cells;

    /**
     * Constructor
     */
    public SSTableModel(final int numRow, final int numCol) {
        super();
        this.numRow = numRow;
        this.numCol = numCol;

        this.cells = new Cell[this.numRow][this.numCol];

        for (int row = 0; row < numRow; row++) {
            for (int col = 0; col < numCol; col++) {
                cells[row][col] = new Cell();
            }
        }
    }

    @Override
    public int getColumnCount() {
        return this.numCol;
    }


    @Override
    public int getRowCount() {
        return this.numRow;
    }

    @Override
    public Object getValueAt(final int rowIndex, final int columnIndex) {
        return cells[rowIndex][columnIndex];
    }

    @Override
    public void setValueAt(final Object aValue, final int rowIndex, final int columnIndex) {

        if (aValue instanceof Cell){
            cells[rowIndex][columnIndex] = (Cell) aValue;
        } else if (aValue instanceof String){
            cells[rowIndex][columnIndex].setText((String) aValue);
        }

        fireTableCellUpdated(rowIndex, columnIndex);
    }

    @Override
    public boolean isCellEditable(final int rowIndex, final int columnIndex) {
        return true;
    }

    @Override
    public Class<?> getColumnClass(final int columnIndex) {
        return Cell.class;
    }
}
